Shader "Custom/GaussianBlur"
{
    Properties
    {
        _MainTex ("Texture", 2D) = "white" {}
        _BlurSize ("Blur Size", Float) = 1.0
        _Direction ("Direction", Vector) = (1, 0, 0, 0) // 默认水平方向
    }
    SubShader
    {
        Tags
        {
            "RenderType"="Opaque"
        }
        LOD 100

        Pass
        {
            CGPROGRAM
            #pragma vertex vert
            #pragma fragment frag
            #include "UnityCG.cginc"

            struct appdata
            {
                float4 vertex : POSITION;
                float2 uv : TEXCOORD0;
            };

            struct v2f
            {
                float2 uv : TEXCOORD0;
                float4 pos : SV_POSITION;
            };

            sampler2D _MainTex;
            float4 _MainTex_ST;
            float _BlurSize;
            float2 _Direction;

            v2f vert(appdata v)
            {
                v2f o;
                o.pos = UnityObjectToClipPos(v.vertex);
                o.uv = TRANSFORM_TEX(v.uv, _MainTex);
                return o;
            }

            fixed4 frag(v2f i) : SV_Target
            {
                float2 uv = i.uv;
                float2 dir = _Direction * _BlurSize;

                // 高斯模糊的权重和偏移量
                float weights[5] = {0.227027, 0.1945946, 0.1216216, 0.054054, 0.016216};
                float2 offsets[5] = {
                    float2(0.0, 0.0), float2(dir.x, dir.y), float2(dir.x * 2, dir.y * 2),
                    float2(dir.x * 3, dir.y * 3), float2(dir.x * 4, dir.y * 4)
                };

                fixed4 col = tex2D(_MainTex, uv) * weights[0];
                for (int j = 1; j < 5; j++)
                {
                    col += tex2D(_MainTex, uv + offsets[j]) * weights[j];
                    col += tex2D(_MainTex, uv - offsets[j]) * weights[j];
                }

                return col;
            }
            ENDCG
        }
    }
    FallBack "Diffuse"
}